package interfaces.options;

import java.util.Collection;

import org.fenggui.StandardWidget;
import org.fenggui.decorator.background.PlainBackground;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.layout.StaticLayout;
import org.fenggui.util.Color;

import fileHandling.language.interfaces.Validation;

import interfaces.GUISource;
import interfaces.WordWrapHelper;
import interfaces.language.LanguageWindow;
import interfaces.menu.AbsMenu;
import interfaces.superWidgets.InterfaceButton;
import interfaces.superWidgets.InterfaceLabel;

public class TextInputErrorWindow extends LanguageWindow {
	
	private StandardWidget focusWidget;
	private AbsMenu menu;
	
	public TextInputErrorWindow(TextInputError error, AbsMenu menu) {
		this(error.getErrorText(), error.getTextOption(), menu);
	}
	
	public TextInputErrorWindow(String errorMessage, StandardWidget focusWidget, AbsMenu menu) {
		super(false, Validation.InputError);
		this.menu = menu;
		this.focusWidget = focusWidget;
		
		getAppearance().add(new PlainBackground(Color.LIGHT_GRAY));
		
		content.setLayoutManager(new StaticLayout());
		
		int width = menu.getWidth() / 3;
		int height = menu.getHeight() / 5;
		int x = (menu.getWidth() / 2) - (width / 2);
		int y = (menu.getHeight() / 2) - (height / 2);
		
		setSize(width, height);
		setXY(x, y);
		layout();
		
		int buttonHeight = content.getHeight() / 3;
		int buttonWidth = content.getWidth();
		
		InterfaceLabel errorLabel = new InterfaceLabel(GUISource.middleFont);
		errorLabel.setWidth(content.getWidth());
		Collection<InterfaceLabel> labels = WordWrapHelper.getCentricLabels(errorLabel, errorMessage);
		
		int i = 1;
		for(InterfaceLabel label : labels) {
			label.setY(content.getHeight() - i * label.getHeight());
			content.addWidget(label);
			i++;
		}
		
		InterfaceButton okButton = new InterfaceButton("OK", GUISource.bigFont);
		okButton.setSize(buttonWidth, buttonHeight);
		int buttonX = 0;
		int buttonY = 0;
		okButton.setXY(buttonX, buttonY);
		okButton.addButtonPressedListener(getOKButtonListener());
		content.addWidget(okButton);
		
		layout();
	}
	
	private IButtonPressedListener getOKButtonListener() {
		return new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				menu.getMenuState().getGUIDisplay().setFocusedWidget(focusWidget);
				close();
			}
		};
	}
}